/**
 * @ProjectName "Smart Ticketing System"
 * @FileName "main.js"
 * @FilePath "./scripts/"
 * @Author "Mohammed Talas"
 * @Email "Talas9@gmail.com"
 * @Client "Al nakaha LLC."
 * @Copyright "2013"
 */
window.cp=function(){$.browser.chrome=/chrom(e|ium)/.test(navigator.userAgent.toLowerCase());if($.browser.chrome)if(window.webkitNotifications.checkPermission()==0)return 2;else if(webkitNotifications.checkPermission()==2)return 3;else return 1;else return 0}
var d=new Date;window.utc=$("head").attr("utc")*1;
window.server_utc=new Date(window.utc+d.getTimezoneOffset()*6e4);
setInterval(function(){window.server_utc.setTime(window.server_utc.getTime()+1e3);},1e3);
window.globalError = function (e) {
	if(window.cp() == 2){
		a = window.webkitNotifications.createNotification('images/icons/large/grey/alert_2.png', 'Error!', e);
		a.show();
		setTimeout(function(){a.cancel()},5e3)
	}else{
		var t = "rand-" + (1 + Math.floor(Math.random() * 9e10));
		var n = '<div id="' + t + '" style="display:none" class="Global-Error">';
		n += '<img width="24" height="24" src="images/icons/small/grey/alert.png">';
		n += "<span>" + e + "</span>";
		n += "</div>";
		$("body>div.Global-Error").hide("blind", {}, 5e2, function () {
			$("body").switchClass("Error-Body", "", 1e3);
			$(this).remove()
		});
		$("body").prepend(n).switchClass("", "Error-Body", 4e0, null, function () {
			$("div#" + t).show("slide", {}, 5e2)
		});
		correctAlerts();
		setTimeout(function(){
			$("div#" + t).hide("blind", {}, 5e2, function () {
				$("body").switchClass("Error-Body", "", 1e3);
				$(this).remove()
			})
		}, 5e3)
	}
}
window.buildClock=function(e,t){setInterval(function(){var n=new Object;var r=new Object;n[e]=new Date(window.server_utc.getTime());var i=t*36e5;n[e].setTime(n[e].getTime()+i);var s=n[e].getSeconds();$("#sec_"+e).html((s<10?"0":"")+s);var o=n[e].getMinutes();$("#min_"+e).html((o<10?"0":"")+o);var u=n[e].getHours();$("#hours_"+e).html((u<10?"0":"")+u);$("div#fdate_"+e).html(window.dayNames[n[e].getDay()]+" "+n[e].getDate()+" "+window.monthNames[n[e].getMonth()]+" "+n[e].getFullYear())},1e3)}
jQuery.download=function(e,t,n){if(e&&t){t=typeof t=="string"?t:jQuery.param(t);var r="";jQuery.each(t.split("&"),function(){var e=this.split("=");r+='<input type="hidden" name="'+e[0]+'" value="'+e[1]+'" />'});jQuery('<form action="'+e+'" method="'+(n||"GET")+'">'+r+"</form>").appendTo("body").submit().remove()}}
function correctAlerts(){$(".alert.dismissible").on("click",function(){$(this).animate({opacity:0},"slow",function(){$(this).slideUp()})})}
function insertStickyAlert(e){var t='<div class="alert dismissible alert_orange">';t+='<img width="24" height="24" src="images/icons/small/white/alert.png">';t+=e;t+="</div>";$("div#ajax_result").append(t);correctAlerts()}
function insertAutoAlert(e,t,n){if(n===undefined)n=5;if(t===undefined)t="";var r="rand-"+(1+Math.floor(Math.random()*999999999));var i='<div id="'+r+'" error="'+t+'" class="alert dismissible alert_orange">';i+='<img width="24" height="24" src="images/icons/small/white/alert.png">';i+=e;i+="</div>";$("div#ajax_result").append(i);correctAlerts();setTimeout(function(){$("div#"+r).click()},n*1e3)}
function insertStickyError(e,t){if(t===undefined)t='';var n='<div error="'+t+'" class="alert dismissible alert_red">';n+='<img width="24" height="24" src="images/icons/small/white/alert.png">';n+=e;n+="</div>";$("div#ajax_result").append(n);correctAlerts()}
function insertAutoError(e,t,n){if(n===undefined)n=5;if(t===undefined)t="";var r="rand-"+(1+Math.floor(Math.random()*999999999));var i='<div id="'+r+'" error="'+t+'" class="alert dismissible alert_red">';i+='<img width="24" height="24" src="images/icons/small/white/alert.png">';i+=e;i+="</div>";$("div#ajax_result").append(i);correctAlerts();setTimeout(function(){$("div#"+r).click()},n*1e3)}
function insertStickySuccess(e,t){if(t===undefined)t='';var n='<div error="'+t+'" class="alert dismissible alert_green">';n+='<img width="24" height="24" src="images/icons/small/white/facebook_like.png">';n+=e;n+="</div>";$("div#ajax_result").append(n);correctAlerts()}
function insertAutoSuccess(e,t,n){if(n===undefined)n=5;if(t===undefined)t="";var r="rand-"+(1+Math.floor(Math.random()*999999999));var i='<div id="'+r+'" error="'+t+'" class="alert dismissible alert_green">';i+='<img width="24" height="24" src="images/icons/small/white/facebook_like.png">';i+=e;i+="</div>";$("div#ajax_result").append(i);correctAlerts();setTimeout(function(){$("div#"+r).click()},n*1e3)}
function insertStickyInfo(e,t){if(t===undefined)t='';var n='<div error="'+t+'" class="alert dismissible alert_green">';n+='<img width="24" height="24" src="images/icons/small/white/alert_2.png">';n+=e;n+="</div>";$("div#ajax_result").append(n);correctAlerts()}
function insertAutoInfo(e,t,n){if(n===undefined)n=5;if(t===undefined)t="";var r="rand-"+(1+Math.floor(Math.random()*999999999));var i='<div id="'+r+'" error="'+t+'" class="alert dismissible alert_green">';i+='<img width="24" height="24" src="images/icons/small/white/alert_2.png">';i+=e;i+="</div>";$("div#ajax_result").append(i);correctAlerts();setTimeout(function(){$("div#"+r).click()},n*1e3)}
window.ready = function(){
	$('body').click(function(){if(window.cp() == 1)window.webkitNotifications.requestPermission()});
	$(".chosen-nosearch").chosen({disable_search_threshold: 50,display_disabled_options: false});
	$(".chosen").chosen({display_disabled_options: false});
	$(".clock_box").each(function(){window.buildClock($(this).attr('city'),$(this).attr('change'))});
	$("a#logout").click(function(){window.start=$("span#logout_interval b").html();window.logout_timer=setInterval(function(){if(window.start==0){window.ajax=$.ajax({url:"./ajax?r=logout",type:"GET",sync:true,success:function(e){if(!e["error"]||e["error"]==0)location.reload()}})}else if(window.start>0){window.start--;$("span#logout_interval b").html(window.start)}},1e3)});$("button#cancel_logout").click(function(){clearInterval(window.logout_timer);$("span#logout_interval b").html(10);return true});
	$(".ajax_form").submit(function(e){
		e.preventDefault();
		var id = $(this).attr('id');
		var request = $(this).attr('request');
		var url = $(this).attr('action')+'?r='+request;
		var method = $(this).attr('method');
		var data = $(this).serializeArray();
		var done = $(this).attr('done');
		var onceForm = $(this).hasClass('once-form');
		window.formNeedSubmit = $("#"+id);
		if(onceForm && done == 'true') insertAutoAlert('There is nothing changed to be saved!');
		else window.ajax = $.ajax({
	            type: method,
	            url: url,
	            data: data,
	            beforeSend: function (xhr) {
	            	window.submitOldText = $("button#submit span").html();
			    	$("button#submit span").html("Please wait....").parent().prop("disabled", true);
			  	},
			  	fail: function(data){
			  		insertStickyError('Internal Error [AJAX Failure], Please call Administrator!',500);
			  		$("button#submit span").html(window.submitOldText).parent().prop("disabled", false);
			  	},
			  	error: function(data){
			  		insertStickyError('Internal Error [AJAX Error], Please call Administrator!',500);
			  		$("button#submit span").html(window.submitOldText).parent().prop("disabled", false);
			  	},
	            success: function(data) {
	            	window.data = data;
	            	$("button#submit span").html(window.submitOldText).parent().prop("disabled", false);
	            	if((data[0]['data'] === undefined) || ($.type(data) == "string")){
	            		var data = new Array();
	            		data[0] = {'error': 500,'data': 'Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: ' + window.data + '-->'};
	            	}
	            	$.each(data, function(index, value) {
	            		if(value['error']){
	            			console.log("Error: " + value['error'] + "\nData: " + value['data'] + "\n");
	            			insertStickyError(value['data'],value['error']);
	            		}else{
	            			$('.ajax_form').attr('done','true');
	            			window.formNeedSubmit = '';
		        			insertAutoSuccess(value['data']);
	            		}
					});
					$('html, body').animate({scrollTop: $("#ajax_result").offset().top});
	            }
	        });
	});
	function convertDate(e){var t=e;var n=e=="hijri"?"greg":"hijri";var r=e=="hijri"?"Gregorian":"Hijri";var i=e=="hijri"?"ajax?r=CONVERT_TO_GREG":"ajax?r=CONVERT_TO_HIJRI";var s=$("#"+t+"_y").val();var o=$("#"+t+"_m").val();var u=$("#"+t+"_d").val();var a=u+"-"+o+"-"+s;if(window.convertingnow==true){return false}if(s<1||!s||o<1||!o||u<1||!u||(s<1301||s>1500)&&t=="hijri"||(s<1883||s>2076)&&t=="greg"){return false}window.ajax=$.ajax({type:"GET",url:i,data:{date:a},beforeSend:function(e){window.convertingnow=true;$("h2#"+n+"_title").html("Please Wait...");$("#"+n+"_y").val("----");$("#"+n+"_m").val("--");$("#"+n+"_d").val("--")},success:function(e){window.data=e;window.convertingnow=false;$("h2#"+n+"_title").html(r);if(e[0]["data"]===undefined||$.type(e)=="string"){var e=new Array;e[0]={error:500,data:"Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: "+window.data+"-->"}}else{$("#"+n+"_y").val(e[0]["data"][2]);$("#"+n+"_m").val(e[0]["data"][1]);$("#"+n+"_d").val(e[0]["data"][0])}}})}$("#hijri_d").keypress(function(e){if(e.keyCode==13)convertDate("hijri")});$("#greg_d").keypress(function(e){if(e.keyCode==13)convertDate("greg")});$("div#date_converter [name=day]").keyup(function(e){origin=$(this).attr("id").split("_");if($(this).val().length==2&&!$.isNumeric($(this).val()))$(this).val($(this).attr("dalue"));if($(this).val()>31&&$(this).val().length==2)$(this).val(31);else if($(this).val()<1&&$(this).val().length==2)$(this).val(1);else if($(this).val().length==2&&(e.keyCode>95&&e.keyCode<106||e.keyCode>47&&e.keyCode<58)){$(this).select();convertDate(origin[0])}else if($(this).val().length==1&&!(e.keyCode>95&&e.keyCode<106)&&!(e.keyCode>47&&e.keyCode<58)&&!(e.keyCode==13))$(this).val("")});$("div#date_converter [name=month]").keyup(function(e){if($(this).val().length==2&&!$.isNumeric($(this).val()))$(this).val($(this).attr("dalue"));if($(this).val()>12&&$(this).val().length==2)$(this).val(12);else if($(this).val()<1&&$(this).val().length==2)$(this).val(1);else if($(this).val().length==2&&$(this).attr("id")=="greg_m"&&e.keyCode!=9||$(this).val().length>=1&&e.keyCode==13&&$(this).attr("id")=="greg_m")$("input#greg_d").select();else if($(this).val().length==2&&$(this).attr("id")=="hijri_m"&&e.keyCode!=9||$(this).val().length>=1&&e.keyCode==13&&$(this).attr("id")=="hijri_m")$("input#hijri_d").select();else if($(this).val().length==1&&!(e.keyCode>95&&e.keyCode<106)&&!(e.keyCode>47&&e.keyCode<58)&&!(e.keyCode==13))$(this).val("")});$("div#date_converter #greg_fields [name=year]").keyup(function(e){if($(this).val().length==4&&!$.isNumeric($(this).val()))$(this).val($(this).attr("dalue"));if($(this).val()>2076&&$(this).val().length==4)$(this).val(2076);else if($(this).val()<1883&&$(this).val().length==4)$(this).val(1883);else if($(this).val().length==4&&(e.keyCode>95&&e.keyCode<106||e.keyCode>47&&e.keyCode<58||e.keyCode==13))$("div#date_converter #greg_fields [name=month]").select();else if($(this).val().length>=1&&!(e.keyCode>95&&e.keyCode<106)&&!(e.keyCode>47&&e.keyCode<58)&&!(e.keyCode==13))$(this).val("")});$("div#date_converter #hijri_fields [name=year]").keyup(function(e){if($(this).val().length==4&&!$.isNumeric($(this).val()))$(this).val($(this).attr("dalue"));if($(this).val()>1500&&$(this).val().length==4)$(this).val(1500);else if($(this).val()<1301&&$(this).val().length==4)$(this).val(1301);else if($(this).val().length==4&&(e.keyCode>95&&e.keyCode<106||e.keyCode>47&&e.keyCode<58||e.keyCode==13))$("div#date_converter #hijri_fields [name=month]").select();else if($(this).val().length>=1&&!(e.keyCode>95&&e.keyCode<106)&&!(e.keyCode>47&&e.keyCode<58)&&!(e.keyCode==13))$(this).val("")});$("div#date_converter input").mouseup(function(e){e.preventDefault();$(this).select()})
	function getPJS(){var f=($("form#login_form").length>0?"/login":location.pathname).split("/");f=f[f.length-1].split(".sts")[0];f=f.length>1?f:"index";$.get("pjs/?js="+f,function(d){if(d["E"])$.each(d["E"],function(i,v){eval(v)});if(d["L"])$.each(d["L"],function(e,t){console.log(t)})})}
	getPJS();
}
function beats() {
	var a = location.pathname.split('/');
	a = a[a.length-1].toUpperCase();
	if(a != 'LOGIN'){
		var e = 0;
		var t = $("head").attr("tabid");
		window.beatser = setInterval(function () {e++}, 1000);
		window.pings = function(){
			$.get("ajax?t=" + t + "&r=ping&b=" + e + "&l=" + location.href);
			window.pingInterval = setInterval(function () {
				if (e < 15000)
					$.get("ajax?t=" + t + "&r=ping&b=" + e + "&l=" + location.href,function(data) {
						if(data[0]['error'] == 403){
							window.pings = clearInterval(window.beatser);
							clearInterval(window.pingInterval);
						}
					});
				else {
					window.pings = clearInterval(window.beatser);
					clearInterval(window.pingInterval);
				}
			}, 15000)
		};
		window.pings();
		$("body").click(function(){e = 0})
	}
}
$(function(){beats();$(document).on("ready pjax:success",function(){window.ready()});$(document).on("pjax:timeout",function(){pjax_timeout()})})
window.monthNames=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];window.dayNames=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];